<?php


/**
 * Easily sort each record based on position
 *
 * @package     csDoctrineSortablePlugin
 * @subpackage  listener
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @link        www.phpdoctrine.org
 * @since       1.0
 * @version     $Revision$
 * @author      Travis Black <tblack@centresource.com>
 */
class Doctrine_Template_Listener_Signable extends Doctrine_Record_Listener {
/**
 * Array of sortable options
 *
 * @var array
 */
  protected $_options = array();


  /**
   * __construct
   *
   * @param array $options
   * @return void
   */
  public function __construct(array $options) {
    $this->_options = $options;
  }


  /**
   * preInsert
   *
   * @param object $Doctrine_Event.
   * @return void
   */
  public function preInsert(Doctrine_Event $event) {
    if(!$this->_options['created']['disabled']) {
      $createdName = $this->_options['created']['name'];
      $event->getInvoker()->$createdName = $this->getUserId('created');
    }

    if(!$this->_options['updated']['disabled'] && $this->_options['updated']['onInsert']) {
      $updatedName = $this->_options['updated']['name'];
      $event->getInvoker()->$updatedName = $this->getUserId('updated');
    }
  }

  /**
   * preUpdate
   *
   * @param object $Doctrine_Event.
   * @return void
   */
  public function preUpdate(Doctrine_Event $event) {
    if( ! $this->_options['updated']['disabled']) {
      $updatedName = $this->_options['updated']['name'];
      $event->getInvoker()->$updatedName = $this->getUserId('updated');
    }
  }

  /**
   * getUserId
   *
   * Gets the userid or username depending on field format
   *
   * @param string $type.
   * @return void
   */
  public function getUserId($type) {
  // В режиме CLI возвращает null
    if (0 != strncasecmp(PHP_SAPI, 'cli', 3)) {
      
      $options = $this->_options[$type];

      if ($options['expression'] !== false && is_string($options['expression'])) {
        return new Doctrine_Expression($options['expression']);
      } elseif (!class_exists("pakeApp")) {
        switch($options['type']) {
          case 'integer':
            if (class_exists('sfGuardUser')) {
              return sfContext::getInstance()->getUser()->getAttribute('user_id', null, 'sfGuardSecurityUser');
            } else {
              return sfContext::getInstance()->getUser()->getId();
            }
            break;
          case 'string':
            return sfContext::getInstance()->getUser()->getUsername();
            break;
          default:
            return 'n/a';
            break;
        }
      }
    }
    else {
      return null;
    }
  }
}
